import request from 'common/request/index.js'
import utils from 'common/utils/index.js'

import store from '..'
import { BASE_URL, SIGN } from '@/env.js';
// #ifdef H5
const jweixin = require('jweixin-module')
//import wxoaShare from '@/common/utils/share.js'
// #endif

export default {
    namespaced: true,
    state: {
        config: "",
        appid: "",
        title: "",
        copyright: [],
        richtext: "",
        options: "",
        share: "",
        jwx: false,
        iosIsPay: false,
        updateInfo: {},
        videoAutoplay: 0,
        playletShare: {
            id: "",
            title: "",
            image: "",
            desc: ""
        }
    },
    getters: {
        config: state => state.config,
        appid: state => state.appid,
        title: state => state.title,
        copyright: state => state.copyright,
        richtext: state => state.richtext,
        options: state => state.options,
        share: state => state.share,
        jwx: state => state.jwx,
        iosIsPay: state => state.iosIsPay,
        updateInfo: state => state.updateInfo,
        videoAutoplay: state => state.videoAutoplay
    },
    mutations: {
        setConfig(state, data) {
            state.config = data
        },
        setAppid(state, data) {
            state.appid = data
        },
        setTitle(state, data) {
            state.title = data
        },
        setCopyright(state, data) {
            state.copyright = data
        },
        setRichtext(state, data) {
            state.richtext = data
        },
        setOptions(state, data) {
            state.options = data
        },
        setShare(state, data) {
            state.share = data
        },
        setJwx(state, data) {
            state.jwx = data
        },
        setIosIsPay(state, data) {
            state.iosIsPay = data
        },
        setUpdateInfo(state, data) {
            state.updateInfo = data
        },
        setVideoAutoplay(state, data) {
            state.videoAutoplay = data
        }
    },
    actions: {
		 
        // 获取配置信息
        async getConfigInfo({ commit, dispatch, getters, state }, options) {
			//todo
			
			var data= {
			    "system": {
			        "name": "爱视频",
			        "domain": "https:\/\/duanju.nymaite.cn\/pc\/",
			        "h5": "https:\/\/duanju.nymaite.cn\/h5\/",
			        "h5_theme": "default",
			        "version": "1.3.1",
			        "logo": "https:\/\/mtduanju.oss-cn-beijing.aliyuncs.com\/uploads\/20230731\/3722a5abfe39e847b19beb1d1a41164d.png",
			        "company": "https:\/\/mtduanju.oss-cn-beijing.aliyuncs.com\/uploads\/20230721\/5b04e2b7fb8117429b3ea75c4e5910b4.png",
			        "copyright": [
			            {
			                "image": "",
			                "name": "",
			                "url": "https:\/\/www.nymaite.cn"
			            },
			            {
			                "image": "",
			                "name": "备案编号：豫ICP备20007476号-2",
			                "url": "https:\/\/beian.miit.gov.cn"
			            }
			        ],
			        "mobile_switch": "1",
			        "android_autoplay": "1",
			        "mobile": "13718188679",
			        "email": "1038607463@qq.com",
			        "wechat": "kaifa8898",
			        "user_protocol": 42,
			        "privacy_protocol": 36,
			        "about_us": 37,
			        "contact_us": 35,
			        "legal_notice": 38,
			        "usable_desc": 41,
			        "vip_desc": 40,
			        "reseller_desc": 39
			    },
			    "share": {
			        "title": "迈特短剧",
			        "description": "迈特短剧SaaS多用户系统！！！",
			        "image": "https:\/\/mtduanju.oss-cn-beijing.aliyuncs.com\/uploads\/20230731\/3722a5abfe39e847b19beb1d1a41164d.png",
			        "user_poster_bg": "https:\/\/mtduanju.oss-cn-beijing.aliyuncs.com\/uploads\/20231114\/51b483231ef409569e570970d2331c20.jpg",
			        "user_poster_bg_color": "#4CAF50",
			        "msg_title_bg": "https:\/\/aisite2.nymaite.cn\/uploads\/20230721\/5b04e2b7fb8117429b3ea75c4e5910b4.png",
			        "msg_title_bg_color": "#9C27B0"
			    },
			    "payment": [],
			    "wechat": {
			        "appid": "wxa411517fbd1c1ff9"
			    },
			    "apple_pay": "0",
			    "uniad_switch": "1",
			    "adpid": "adunit-fc2ea8d5a915e814",
			    "user": {
			        "nickname": "Mett -",
			        "avatar": "\/uploads\/20230731\/3722a5abfe39e847b19beb1d1a41164d.png",
			        "group_id": "1",
			        "money": "",
			        "score": ""
			    },
			    "category": [
			        {
			            "id": 1206,
			            "site_id": 2,
			            "name": "视频分类",
			            "style": 1,
			            "type": "video",
			            "image": null,
			            "pid": 0,
			            "weigh": 0,
			            "description": ""
			        },
			        {
			            "id": 1220,
			            "site_id": 2,
			            "name": "视频年份",
			            "style": 1,
			            "type": "year",
			            "image": null,
			            "pid": 0,
			            "weigh": 0,
			            "description": ""
			        },
			        {
			            "id": 1230,
			            "site_id": 2,
			            "name": "视频地区",
			            "style": 1,
			            "type": "area",
			            "image": null,
			            "pid": 0,
			            "weigh": 0,
			            "description": ""
			        }
			    ]
			}
			
			
			
			
			
            // const result = await request("common.init", {
            //     platform: utils.platforms() || 'H5'
            // })
			
           // if (result.code === 1) {
                commit("setConfig",data)

                if (utils.platforms() === 'wxOfficialAccount' || utils.platforms() === 'wxMiniProgram') {
                    commit("setAppid",data.wechat.appid)
                }

                const isIos = uni.getSystemInfoSync().osName == 'ios' ? true : false
                const apay =data.apple_pay == 0 ? false : true
                commit("setIosIsPay", !apay && isIos ? false : true)
                commit("setTitle",data.system.name)
                commit("setCopyright",data.system.copyright)
                commit("setRichtext",data.system)

                // #ifdef H5
                document.title =data.system.name

                if (utils.platforms() === 'wxOfficialAccount') {
                    commit("setVideoAutoplay",data.system.android_autoplay)
                }

                // utils.platforms() == 'wxOfficialAccount' && dispatch('getWxShareConfigInfo')
                // utils.platforms() == 'wxOfficialAccount' && wxoaShare()
                // #endif

                return data
           // }
            return false
        },
        // 微信分享配置信息
        async getWxShareConfigInfo({ commit, dispatch, getters, state }) {
            // #ifdef H5
            // const spm = store.state.user.userInfo ? .id ? `${store.state.user?.userInfo?.id}.1.0.2.1` : ""
            // const title = state.config ? .share ? .title ? state.config ? .share ? .title : ""
            // const imgUrl = state.config ? .share ? .image ? state.config ? .share ? .image : ""
            // const desc = state.config ? .share ? .description ? state.config ? .share ? .description : ""
			 const spm ="";
			 const title ="";
			 const imgUrl ="";
			  const desc ="";
            const uri = window.location.origin + window.location.pathname
            const link = uri + `?${SIGN}#/pages/home/index?scene=${spm}`

            // request('user.share', { uri }).then(res => {
            //         if (res.code === 1) {
            //             console.log("获取分享配置");
            //             jweixin.config({
            //                 debug: false,
            //                 appId: res.data.appId,
            //                 nonceStr: res.data.nonceStr,
            //                 timestamp: res.data.timestamp,
            //                 signature: res.data.signature,
            //                 jsApiList: [
            //                     'updateAppMessageShareData',
            //                     'updateTimelineShareData',
            //                 ]
            //             })
            //             jweixin.ready(() => {
            //                 // true(已登录) false(未登录)
            //                 commit('setJwx', spm ? true : false)
            //                     // 分享给朋友
            //                 jweixin.updateAppMessageShareData({
            //                         title,
            //                         desc,
            //                         link,
            //                         imgUrl,
            //                         success: e => {
            //                             console.log("分享好友");
            //                         }
            //                     })
            //                     // 分享朋友圈
            //                 jweixin.updateTimelineShareData({
            //                     title,
            //                     link,
            //                     imgUrl,
            //                     success: e => {
            //                         console.log("分享朋友圈");
            //                     }
            //                 })
            //             })
            //             jweixin.error(err => {
            //                 console.log(err, "微信分享config配置失败");
            //             })
            //         }
            //     })
			
                // #endif
        }
    }
}